<h1>Module Installer</h1>
<?php
	if (local_post()) {
   	$upload_errors =  array('UPLOAD_ERR_OK', 'UPLOAD_ERR_INI_SIZE', 'UPLOAD_ERR_FORM_SIZE', 'UPLOAD_ERR_PARTIAL', 'UPLOAD_ERR_NO_FILE', 'UPLOAD_ERR_NO_TMP_DIR', 'UPLOAD_ERR_CANT_WRITE', 'UPLOAD_ERR_EXTENSION');
    
    if (!empty($_FILES['upload']['error']) && $_FILES['upload']['error'] != 4) error($upload_errors[$_FILES['upload']['error']]);
		elseif (is_uploaded_file($_FILES['upload']['tmp_name'])) {
  		if (is_writeable(PHPDRIVER . 'modules')) {
  				$cmd = escapeshellcmd(UNZIP_CMD . " {$_FILES['upload']['tmp_name']} -d " . PHPDRIVER . 'modules/');
  				shell_exec("$cmd");
					
					$module_name = strtolower(str_replace(' ', '_', basename($_FILES['upload']['name'], '.zip')));
					
					if (!is_dir(PHPDRIVER . "modules/$module_name")) {
						//check for spaces in folder name
						if (is_dir(PHPDRIVER . 'modules/' . str_replace('_', ' ', $module_name))) {
							if (!rename(PHPDRIVER . 'modules/' . str_replace('_', ' ', $module_name), PHPDRIVER . "modules/$module_name")) {
								error('Failed to fix spaces in module folder!');
							}
						}
					}
					
					if (is_dir(PHPDRIVER . "modules/$module_name/database_schema")) {
						foreach (scandir(PHPDRIVER . "modules/$module_name/database_schema") as $inifile) {
							if (substr($inifile, '.') == '.') continue;
							
							if (stristr($inifile, '.ini')) {
								if (file_exists(PHPDRIVER . "database_schema/$inifile")) {
									$ok = (rename(PHPDRIVER . "database_schema/$inifile", PHPDRIVER . 'database_schema/bak_' . microtime(true) . '_' . $inifile)) ? true : false;
								} else $ok = true;
								if ($ok) {
									if (!rename(PHPDRIVER . "modules/$module_name/database_schema/$inifile", PHPDRIVER . "database_schema/$inifile")) {
										error("Failed to move database_schema/$inifile");
									}
								} else error("Failed to backup old database_schema/$inifile");
							}
							unlink(PHPDRIVER . "modules/$module_name/database_schema/$inifile");
						}
						rmdir(PHPDRIVER . "modules/$module_name/database_schema");
					}
					
					db_query("INSERT INTO modules (name, folder, active) VALUES ('" . ucwords(str_replace('_', ' ', $module_name)) . "', '$module_name', 1)");
					
					message('New module installed. :-)');
					
					if (is_file(PHPDRIVER . "modules/$module_name/install.php")) location("/$module_name/install");
					else location('/modules/admin/index');
  		} else error('Please chmod 777 ' . PHPDRIVER . ' modules/');
		} else error('Upload failed.');
	}
?>
<form method="post" enctype="multipart/form-data">
	<fieldset>
		<legend>Install a New Module</legend>
		
		<small>Module Packages consist of a zip file containing exactly one root folder. The name of that folder is the module folder name.</small>
		<br />
		
		<label for="upload">
			Select a &quot;module.zip&quot; file
			<input type="file" name="upload" id="upload" value="" />
		</label>
		
		
		<input type="submit" name="button" value="Upload &amp; Install" />
	</fieldset>
</form>